<template>
  <div>
    <div>
      <div style="display: flex;justify-content: space-between">
        <div>
          <search
            :disabled="showAdvanceSearchView"
            @search="loadUsers"
          />
          <el-button
            type="primary"
            style="margin-left: 8px"
            @click="showAdvanceSearchView = !showAdvanceSearchView"
          >
            <i
              :class="showAdvanceSearchView?'fa fa-angle-double-up':'fa fa-angle-double-down'"
              aria-hidden="true"
            />
            高级搜索
          </el-button>
        </div>
        <div>
          <data-load />
          <el-button
            type="primary"
            icon="el-icon-plus"
            style="margin-left: 8px"
            @click="showAddUserView"
          >
            添加用户
          </el-button>
        </div>
      </div>
      <advance-search
        :visible.sync="showAdvanceSearchView"
        @search="loadUsers"
      />
    </div>
    <user-table
      :users="users"
      :total="total"
      :loading="loading"
      :edit-user="editUser"
      :advance-user="advanceUser"
      :delete-user="deleteUser"
      @change="loadUsers"
      @current-change="currentChange"
      @size-change="sizeChange"
    />
    <user-edit-dialog
      :title="title"
      :user="user"
      :visible.sync="dialogVisible"
      @submit="loadUsers"
    />
  </div>
</template>

<script>
  import UserEditDialog from "./components/UserEditDialog";
  import UserTable from "./components/UserTable";
  import Search from "./components/Search";
  import AdvanceSearch from "./components/AdvanceSearch";
  import DataLoad from "./components/DataLoad";
  //import UserAPI from "@/api/UserAPI"
  import store from "../../../store";
    export default {
        name: "UserBasicManager",
        components:{
            Search,
            AdvanceSearch,
            UserTable,
            DataLoad,
            UserEditDialog
        },
        data() {
            return {
                title: '',
                showAdvanceSearchView: false,
                dialogVisible:false,
                loading: false,
                total: 0,
                page: 1,
                keyword: '',
                size: 10,
                users:[],
                user:{}
            }
        },
        mounted() {
          this.$store.dispatch('notification/connect');
            this.loadUsers();
            this.initData();
        },
        methods: {
            emptyUser(){
                this.user={
                  address: "深圳南山",
                  authorities: [],
                  enable: true,
                  first_time: 1595619639000,
                  last_time: 1595619641000,
                  locked: false,
                  name: "系统管理员",
                  phone: "18568887789",
                  telephone: "029-82881234",
                  userface: "http://bpic.588ku.com/element_pic/01/40/00/64573ce2edc0728.jpg",
                  username: "admin",
                }
            },
            initData() {

            },
            sizeChange(currentSize) {
                this.size = currentSize;
                this.initEmps();
            },
            currentChange(currentPage) {
                this.page = currentPage;
                this.initEmps();
            },
            showAddUserView() {
                this.user  = this.emptyUser();
                this.title = '添加用户';
                this.dialogVisible = true;
            },
            showEditUserView(data) {
                this.title = '编辑用户信息';
                this.user = data;
                this.dialogVisible = true;
            },
            loadUsers(data){
                this.loading = true;
                let params = {
                    page:this.page,
                    size:this.size
                }
                if (data) {
                    if(data[0] === 'advanced'){
                        Object.assign(params,data[1]);
                    }else {
                        Object.assign(params,{
                            nickname:data[1]
                        })
                    }
                }
                // UserAPI.getUsers(params).then(resp => {
                //     this.loading = false;
                //     if (resp) {
                //         this.users = resp.records;
                //         this.total = resp.total;
                //         window.sessionStorage.setItem("users", JSON.stringify(resp));
                //     }
                // });
            },
            editUser(data){
                this.showEditUserView(data)
            },
            advanceUser(data){

            },
            deleteUser(data) {
                this.$confirm('此操作将永久删除【' + data.username + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // UserAPI.deleteUser(data.id).then(resp => {
                    //     if (resp) {
                    //         this.$message({
                    //             type: 'success',
                    //             message: '删除成功'
                    //         });
                    //     }
                    // })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        }
    }
</script>

<style>

</style>
